package com.ml.doctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.google.gson.reflect.TypeToken;
import com.ml.doctor.R;
import com.ml.doctor.bean.ZhangDanBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.LocalShared;
import com.ml.doctor.utils.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AccountActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.txt_gold)
    TextView txtGold;
    @BindView(R.id.swipe_target)
    ListView swipeTarget;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;
    int page;
    ZhangDanAdapter zhangDanAdapter;
    double gold;
    List<ZhangDanBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_layout);
        ButterKnife.bind(this);
        setRightText("提现");
        setTopTitle("我的账户");
        list = new ArrayList<>();
        zhangDanAdapter = new ZhangDanAdapter();
        swipeTarget.setAdapter(zhangDanAdapter);
        swipeToLoadLayout.setOnLoadMoreListener(this);
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setRefreshing(true);
    }

    public void getGold() {
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(this).getUserId() + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "/docter/sel_docter_amount", map, Double.class, new NetworkManager.SuccessCallback<Double>() {
            @Override
            public void onSuccess(Double response) {
                gold = response;
                txtGold.setText(response + "");
            }


        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.show(AccountActivity.this, message, Toast.LENGTH_SHORT);
            }
        });
    }

    public void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(this).getUserId() + "");
        map.put("page", page + "");
        map.put("pagesize", 20 + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "docter/sel_docter_bill", map, new TypeToken<List<ZhangDanBean>>() {
        }.getType(), new NetworkManager.SuccessCallback<List<ZhangDanBean>>() {
            @Override
            public void onSuccess(List<ZhangDanBean> response) {
                if (page == 1) {
                    list.clear();
                }
                list.addAll(response);
                page++;
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
                zhangDanAdapter.notifyDataSetChanged();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(AccountActivity.this, message);
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);

            }
        });
    }

    @Override
    public void onLoadMore() {
        getData();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getGold();
        getData();
    }

    @Override
    protected void onRightTextClick() {
        super.onRightTextClick();
        Intent i = new Intent(this, TiXianActivity.class);
        i.putExtra("gold", gold);
        startActivity(i);
    }

    class ZhangDanAdapter extends BaseAdapter {

        ViewHolder viewHolder;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(AccountActivity.this).inflate(R.layout.item_account_layout, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.tvItemCount.setText("+" + list.get(position).dba);
            viewHolder.tvItemSource.setText(list.get(position).ws);
            viewHolder.tvItemTime.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date(Long.parseLong(list.get(position).time))));
            return convertView;
        }


        class ViewHolder {
            @BindView(R.id.tv_item_source)
            TextView tvItemSource;
            @BindView(R.id.tv_item_time)
            TextView tvItemTime;
            @BindView(R.id.tv_item_count)
            TextView tvItemCount;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
